<?php
/**
 * Created by PhpStorm
 * User: 陈志洪
 * Date: 2020/1/21
 * Time: 21:59
 */

namespace app\api\service;

use app\api\controller\Base;
use think\Cache;
use think\Request;

class TokenService extends Base
{
    // 获取 token
    public function checkToken()
    {
        $token = Request::instance()->header('token');
        if (!$token) {
            parent::error('error', '请登录', 400);
        }

        return $this->isOverdue($token);
    }

    // 是否过期或无效
    private function isOverdue($token)
    {
        $token_info = Cache::get($token);
        if (!$token_info) {
            parent::error('authorization', '请重新登录', 401);
        }

        return $token_info;
    }
}
